﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace Data
{


    /// <summary>
    /// 目录树 双向绑定
    /// </summary>
    public class RequserDirectoryNodeData : INotifyPropertyChanged
    {
        private List<RequserDirectoryData> _NodeData = new List<RequserDirectoryData>();

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 加载本地数据
        /// </summary>
        /// <returns></returns>
        private List<RequserDirectoryData> getSave()
        {

            string thisExeFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisFile = thisExeFile + @"JSONData" + @"\\ItemNode.json";
            if (File.Exists(thisFile))
            {
                using (StreamReader file = File.OpenText(thisFile))
                {
                    Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                    List<RequserDirectoryData> NodeDatalist = (List<RequserDirectoryData>)serializer.Deserialize(file, typeof(List<RequserDirectoryData>));
                    _NodeData = NodeDatalist;
                    return _NodeData;
                }
            }
            else
            {
                return _NodeData;
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="NodeData"></param>
        public void SetNodeData(List<RequserDirectoryData> NodeData) {
            _NodeData = NodeData;
            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisDirectory = thisExeDirectory + @"JSONData";
            if (!Directory.Exists(thisDirectory))
            {
                Directory.CreateDirectory(thisDirectory);
            }
            //保存节点
            string thisExeFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisFile = thisExeFile + @"JSONData" + @"\\ItemNode.json";
            using (StreamWriter file = File.CreateText(thisFile))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file, _NodeData);
            }
        }

        public List<RequserDirectoryData> NodeData
        {
            get { return getSave(); }
            set
            {
                _NodeData = value;
                PropertyChanged(this, new PropertyChangedEventArgs("ind"));
            }
        }
    }
    
    
    /// <summary>
    /// 目录树
    /// </summary>
    [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptOut)]
    public class RequserDirectoryData
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string key { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public string type { get; set; }

        /// <summary>
        /// 打开次数 不保存
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int opendnumber { get; set; }

        #region 节点目录结构一定要放置到最后

        /// <summary>
        /// 当前节点ID
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 父节点ID
        /// </summary>
        public string ParentID { get; set; }

        #endregion

    }
}
